package com.example.sinvoicedemo

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.libra.sinvoice.LogHelper
import com.libra.sinvoice.SinVoicePlayer
import com.libra.sinvoice.SinVoiceRecognition

/**
 * @author zhaokaiqiang
 * @ClassName: com.example.sinvoicedemo.MainActivity
 * @Description: 声波通信
 * @date 2014-11-15 下午12:36:32
 */
class MainActivity : Activity(), SinVoiceRecognition.Listener, SinVoicePlayer.Listener {
    companion object {

        private val TAG = "MainActivity"
        // 最大数字
        private val MAX_NUMBER = 5

    }

    // 播放
    private lateinit var mSinVoicePlayer: SinVoicePlayer
    // 录音
    private lateinit var mRecognition: SinVoiceRecognition
    private lateinit var resTV: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mSinVoicePlayer = SinVoicePlayer()
        mSinVoicePlayer.setListener(this)

        mRecognition = SinVoiceRecognition()
        mRecognition.setListener(this)

        val playText = findViewById<View>(R.id.play_text) as EditText
        resTV = findViewById(R.id.resTV)

        // 开始播放声音
        findViewById<View>(R.id.start_play).setOnClickListener { mSinVoicePlayer.play(playText.text.toString()) }

        // 停止播放声音
        findViewById<View>(R.id.stop_play).setOnClickListener { mSinVoicePlayer.stop() }

        // 开始声音识别
        findViewById<View>(R.id.start_reg).setOnClickListener { mRecognition.start() }

        // 停止声音识别
        findViewById<View>(R.id.stop_reg).setOnClickListener { mRecognition.stop() }
    }


    override fun onRecognitionStart() {
    }

//    override fun onRecognition(ch: Char) {
    //mHanlder!!.sendMessage(mHanlder!!.obtainMessage(MSG_SET_RECG_TEXT, ch.toInt(), 0))
//    }

    override fun onRecognition(result: String) {
        resTV.text = result
    }

    override fun onRecognitionEnd() {
    }

    override fun onPlayStart() {
        // 开始播放音频
        LogHelper.d(TAG, "start play")
    }

    override fun onPlayEnd() {
        // 结束播放
        LogHelper.d(TAG, "stop play")
    }

}
